package com.biubiushop.Socket.TcpAndUdp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author chenjia
 * @date 2019/7/16
 **/
public class SocketClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost",9999);
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("这是客户端发过来的消息");
        bw.flush();
        socket.shutdownOutput();

        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String info = null;
        while ((info = br.readLine())!=null){
            System.out.println("服务端发过来的信息为:"+info);
        }

        socket.shutdownInput();

        br.close();
        isr.close();
        is.close();

        bw.close();
        osw.close();
        os.close();
        socket.close();
    }
}
